package com.example.securitydemos.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;

import javax.sql.DataSource;

/**
 * @author linjy on 2023/1/30
 */
//@EnableWebSecurity(debug = true)
public class SecurityConfig_注销登录的实现 extends WebSecurityConfigurerAdapter {

    @Value("${spring.security.remember-me.key}")
    private String rememberKey;

    @Autowired
    private DataSource dataSource;

    @Autowired
    private UserDetailsService userDetailsService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        JdbcTokenRepositoryImpl tokenRepository = new JdbcTokenRepositoryImpl();
        tokenRepository.setDataSource(dataSource);

        http.authorizeRequests()
                .antMatchers("/admin/**")
                .hasRole("ADMIN")
                .antMatchers("/user/**")
                .hasRole("USER")
                .antMatchers("/app/**")
                .permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .formLogin()
                .permitAll()
                .and()
                //开启记住我功能
                .rememberMe()
                .userDetailsService(userDetailsService)
                //1.散列加密方案
                .key(rememberKey)
                //2.持久化令牌方案
                .tokenRepository(tokenRepository)
                //7天有效期
                .tokenValiditySeconds(60 * 60 * 24 * 7)
                .and()
                //配置退出登录功能
                .logout()
                //关联自己的退出登录接口
                //.logoutUrl("/logout")
                .logoutUrl("/user/logout")
                //注销成功，重定向到该路径下
                .logoutSuccessUrl("/login")
                //与logoutSuccessUrl处理策略类似,但更灵活.
                //.logoutSuccessHandler(new LogoutSuccessHandler() {
                //    @Override
                //    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
                //处理成功退出登录后的业务
                //    }
                //})
                //.addLogoutHandler(new LogoutHandler() {
                //    @Override
                //    public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
                //处理退出登录业务
                //    }
                //})
                //使得session失效
                .invalidateHttpSession(true)
                //清除认证信息
                .clearAuthentication(true)
                //删除指定的cookie
                .deleteCookies("cookie01","cookie02")
                .and()
                .csrf()
                .disable();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return NoOpPasswordEncoder.getInstance();
    }

}
